package com.atguigu.daijia.rules.service.impl;

import com.atguigu.daijia.common.constant.RuleConstant;
import com.atguigu.daijia.model.form.rules.ProfitsharingRuleRequest;
import com.atguigu.daijia.model.form.rules.ProfitsharingRuleRequestForm;
import com.atguigu.daijia.model.vo.rules.ProfitsharingRuleResponse;
import com.atguigu.daijia.model.vo.rules.ProfitsharingRuleResponseVo;
import com.atguigu.daijia.rules.mapper.ProfitsharingRuleMapper;
import com.atguigu.daijia.rules.service.ProfitsharingRuleService;
import com.atguigu.daijia.rules.utils.DroolsHelper;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 谢忠涵7
 */
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class ProfitsharingRuleServiceImpl implements ProfitsharingRuleService {

    /**
     * 计算系统分账费用
     */
    @Override
    public ProfitsharingRuleResponseVo calculateOrderProfitsharingFee(ProfitsharingRuleRequestForm profitsharingRuleRequestForm) {
        //封装规则请求的对象
        ProfitsharingRuleRequest request = new ProfitsharingRuleRequest();
        BeanUtils.copyProperties(profitsharingRuleRequestForm, request);
        //获取规则引擎会话
        KieSession kieSession = DroolsHelper.loadForRule(RuleConstant.PROFITSHARING_RULES_FILE_PATH);
        //定义好全局变量
        ProfitsharingRuleResponse profitsharingRuleResponse = new ProfitsharingRuleResponse();
        //注入请求
        kieSession.insert(request);
        //设置好全局变量
        kieSession.setGlobal("profitsharingRuleResponse",profitsharingRuleResponse);
        //执行全部规则
        kieSession.fireAllRules();
        //关闭会话
        kieSession.dispose();
        //封装返回对象
        ProfitsharingRuleResponseVo profitsharingRuleResponseVo = new ProfitsharingRuleResponseVo();
        BeanUtils.copyProperties(profitsharingRuleResponse,profitsharingRuleResponseVo);
        return profitsharingRuleResponseVo;
    }
}
